1 /**
  2  *
  3  * 多账号绑定手机
  4  *
  5  * @fileOverView
  6  * @author  <a href="mailto:langtao@snda.com">langtao</a>
  7  * @date    2012-12-4
  8  */
  9 ;
 10 define(
 11 	[
 12 		"business/captcha",
 13 		"component/form",
 14 		"component/process_components",
 15 		"component/rsa"
 16 	], 
 17 	function() {
 18 		var _html = '<div class="we_cell_form_box" attr="root"></div>',
 19 			_tips = "请填写绑定的账号和密码",
 20 			_btmTips = "<strong>温馨提示:</strong>手机账号不能重复绑定<br><strong>手机账号:</strong>可以用手机号码作为登录名的账号<br>",
 21 			// _verifyPwdApi = "/common/js/test/business/api.php",
 22 			_verifyPwdApi = $we.conf.ENV.__API + "/ajaxSafe/checkPassword",
 23 			_proc_data = ['need_pwd', 'safety_verify'];
 24 
 25 		/**
 26 		 * 多账号绑定手机组件
 27 		 * @lends  $we.widget.bind_account_to_mobile_group
 28 		 */
 29 		$we.widget.reg("bind_account_to_mobile_group", {
 30 			/**
 31 			 * @constructs
 32 			 */
 33 			init: function(el, params) {
 34 				this.el = el;
 35 				this.params = params || {};
 36 
 37 				this.params.tips = $we.utils.setValue(this.params.tips, _tips);
 38 				this.group = $we.process.getData("group") || "";
 39 			},
 40 			/**
 41 			 * interfaces
 42 			 * @memberOf $we.widget.bind_account_to_mobile_group#
 43 			 */
 44 			interfaces: {
 45 				/**
 46 				 * 渲染
 47 				 * @memberOf $we.widget.bind_account_to_mobile_group#
 48 				 * @param  {Object} params 参数
 49 				 */
 50 				render: function(params) {
 51 					if (typeof params == "object") {
 52 						for (var i in params) {
 53 							this.params[i] = params[i];
 54 						}
 55 					}
 56 
 57 					this.tips = $we.widget.add("process.tips.small", this.el, {
 58 						content: this.params.tips
 59 					});
 60 
 61 					this.append(this.el, _html);
 62 
 63 					var me = this;
 64 					this.form = $we.widget.add("Form", this.node.root, {
 65 	                    form_elements: [{
 66 	                    	label: "当前手机",
 67 							name: 'group',
 68 							type: 'content',
 69 							value: '<span class="we_yellow">'+$we.string.maskString(this.group)+'</span>'
 70 	                    }, {
 71 							label: "账号名",
 72 							name: 'account',
 73 							type: "text",
 74 							events: "blur",
 75 							placeholder: "G家",
 76 							require: "true"
 77 						}, {
 78 							label: "账号密码",
 79 							name: 'password',
 80 							type: "password",
 81 							events: "blur",
 82 							require: "true"
 83 						}],
 84 	                    commit: function() { me.notify("next"); }
 85 					});
 86 
 87 					if ($we.process.getData("captcha_key"))
 88 						this.addCaptcha($we.process.getData("captcha_key"));
 89 					this.notify("renderBottom", _btmTips);
 90 				},
 91 				/**
 92 				 * 添加验证码
 93 				 * @memberOf $we.widget.bind_account_to_mobile_group#
 94 				 * @param {String} captcha_key Captcha Key
 95 				 */
 96 				addCaptcha: function(captcha_key) {
 97 					if (!captcha_key) return;
 98 
 99 					if (this.captChaPic) {
100 						if ($we.process.getData("captcha_key") != captcha_key)
101 							this.captChaPic.changeKey(captcha_key);
102 					} else {
103 						if (!$we.process.getData("captcha_key"))
104 							$we.process.setData("captcha_key", captcha_key);		
105 						this.form.assemble({
106 							form_elements: [{
107 								label: "验证码",
108 								name: "captcha",
109 								type: "text",
110 								require: "true"
111 							}],
112 							append: true
113 						});
114 						
115 						this.captChaPic = $we.widget.add("captcha", this.node.root, $we.process.getData("captcha_key"));	
116 					}
117 				},
118 				/**
119 				 * 验证密码
120 				 * @memberOf $we.widget.bind_account_to_mobile_group#
121 				 */
122 				verifyPwd: function() {
123 					if (this.verifyingPwd || !this.form.valid()) return;
124 					this.verifyingPwd = true;
125 
126 					var me = this,
127 						data = {
128 							account: this.form.getValue("account"),
129 							pwd: $we.rsa.encrypt(this.form.getValue("password"))
130 						};
131 					if ($we.process.getData("captcha_key")) {
132 						// data.captcha_key = $we.process.getData("captcha_key");
133 						data.captcha = this.form.getValue("captcha");
134 					}
135 						
136 					$we.utils.request(_verifyPwdApi, data,
137 						function(data) {
138 						// 储存需要保存的数据
139 						for (var i = 0; i < _proc_data.length; i++) {
140 							if (typeof data.data[_proc_data[i]] != "undefined")
141 								$we.process.setData(_proc_data[i], data.data[_proc_data[i]]);
142 						};
143 
144 						$we.process.setData("alias", me.form.getValue("account"));
145 
146 						// 如果被要求暂停流程
147 						if (data.data.pause && data.data.pause.is_pause) {
148 							if (data.data.pause.phone) {
149 								$we.process.setData("phone", data.data.pause.phone);
150 								$we.process.setData("change_mobile", true);
151 								me.bChangeMobile = true;
152 							}
153 							me.captChaPic && me.captChaPic.change();
154 							me.setMidTips(data.data.pause.msg);
155 							me.notify("setButton", {cancel:true});
156 							me.notify("pause");
157 							me.verifyingPwd = false;
158 							return;
159 						}
160 
161 						me.verifyingPwd = false;
162 						me.notify("goNext", data);
163 					}, function(data) {
164 						if (data.data && data.data.captcha_key) {
165 							me.addCaptcha(data.data.captcha_key);
166 						}
167 
168 						if ($.inArray(data.errno, [-1, -3]) != -1) {
169 							me.form.showError("account", data.msg);
170 						} else if ($.inArray(data.errno, [-4, -5, -6]) != -1) {
171 							me.form.showError("captcha", data.msg);
172 						} else {
173 							me.form.showError("password", data.msg);
174 						}
175 
176 						me.notify("pause");
177 						me.verifyingPwd = false;
178 					}, "POST", true);
179 				},
180 				/**
181 				 * 设置中间的提示
182 				 * @memberOf $we.widget.bind_account_to_mobile_group#
183 				 * @param {String} msg 提示信息
184 				 */
185 				setMidTips: function(msg) {
186 					if (this.midTips) this.midTips.remove();
187 					this.midTips = $we.widget.add("process.tips.small", this.el, {
188 						content: msg
189 					});
190 				}
191 			},
192 			/**
193 			 * process
194 			 * @memberOf $we.widget.bind_account_to_mobile_group#
195 			 */
196 			process: {
197 				start: function(params) {
198 					this.render(params);
199 				},
200 				end: function() {
201 					$(this.node.root).remove();
202 					this.tips && this.tips.remove();
203 					this.midTips && this.midTips.remove();
204 				},
205 				checkSucc: function() {
206 					if (this.bChangeMobile)
207 						this.notify("goNext");
208 					else
209 						this.verifyPwd();
210 				}
211 			}
212 		});
213 
214 		var _limitHtml = [
215 			'<div class="we_cell_alert_tips_b"><div style="padding-top:9px">手机 $phone$ 绑定账号数已经到达上限,请解绑账号</div></div>',
216 	    	'<div class="we_align_center">',
217 	            '<img src="'+$we.conf.ENV.__STATIC+'/img/src/unbind_pic1.png?v='+$we.STATIC_VERSION+'">',
218 	        '</div>',
219 	        '<div class="we_cell_submit_box">',
220 	        	'<a attr="click:close" href="javascript:void(0)" class="we_button">我知道了</a>',
221 		    '</div>'
222 		].join("");
223 
224 		$we.widget.reg("bind_account_to_mobile_group.limit", {
225 			init: function() {
226 				this.extend("NoticeDialog", [{
227 					title: '绑定账号',
228 					content: $we.string.replaceTmpl(_limitHtml, {
229 						phone: $we.process.getData("group") || ""
230 					}),
231 					width: 540
232 				}]);
233 			}
234 		});
235 
236 		var _endHtml = [
237 				'<div class="we_cell_status">',
238 	              '<p class="we_txt"><strong class="we_cell_title"><i class="we_icon_suc_b"></i>绑定账号$bind_account$成功!</strong></p>',
239 	        	'</div>',
240 	            '<div class="we_cell_bind_acc">',
241 	            	'<div class="we_cell_bind_acc_box">',
242 	                    '<p class="we_phone_number">$group$</p>',
243 	                    '<p class="we_acc_name">$bind_account$</p>',
244 	                '</div>',
245 	            '</div>',
246 	            '<div class="we_cell_func_intro">',
247 	            	'<p class="we_mb20">下载并安装 <a target="_blank" href="http://g.sdo.com">G家</a> ,该账号即可享受以下服务</p>',
248 	            	'<ul>',
249 	                	'<li class="we_func_item">',
250 	                    	'<h3 class="we_func_title"><i class="we_icon_key_login"></i>一键登录</h3>轻滑手机即可登录,安全又便捷',
251 	                    '</li>',
252 	                    '<li class="we_func_item">',
253 	                    	'<h3 class="we_func_title"><i class="we_dyn_pwd"></i>动态密码</h3>会变的密码,让账户与消费更安心',
254 	                    '</li>',
255 	                    '<li class="we_func_item">',
256 	                    	'<h3 class="we_func_title"><i class="we_safe_notice"></i>安全通知</h3>即时的账号、消费安全通知一手掌握',
257 	                    '</li>',
258 	                '</ul>',
259 	            '</div>'
260 			].join("");
261 
262 		$we.widget.reg("bind_account_to_mobile_group.end", {
263 			init: function(el, config) {
264 				this.el = el;
265 				this.config = config;
266 			},
267 			interfaces: {
268 				render: function(params) {
269 					this.append(this.el, _endHtml, {
270 						group: $we.process.getData("group"),
271 						bind_account: $we.string.maskString($we.process.getData("alias"))
272 					});
273 
274 					this.notify("setButton", {next: "完成"});
275 				}
276 			},
277 			process: {
278 				start: function(params) { this.render(params); },
279 				end: function() {
280 					this.config.success();
281 					$(this.node.root).remove();
282 				},
283 				checkSucc: function() { this.notify("close");}	
284 			}
285 		});
286 
287 		return $we.widget.amd("bind_account_to_mobile_group");
288 	}
289 );