1 /**
  2  *
  3  * 修改密码
  4  *
  5  * @fileOverView
  6  * @author  <a href="mailto:zhang.gd@foxmail.com">Zhang Guangda</a>
  7  * @date    2012-10-25
  8  */
  9 define(
 10 	[
 11 		"component/process_components",
 12 		"component/verify_code",
 13 		"component/rsa",
 14 		"business/pwd_strength"
 15 	], 
 16 	function() {
 17 		var _html = '<div class="we_cell_form_box" attr="root"></div>',
 18 			_changeProcessHtml = '<a style="float:right;margin-top:10px;" href="javascript:void(0)" attr="inner:changeBtn;click:changeProcess">通过其它方式修改>></a>',
 19 			_defaultUrl = $we.conf.ENV.__API + "/ajaxVcode/checkMobile";
 20 
 21 		/**
 22 		 * 修改密码组件
 23 		 * @lends  $we.widget.modify_password_by_phone
 24 		 */
 25 		$we.widget.reg("modify_password_by_phone", {
 26 			/**
 27 			 * @constructs
 28 			 */
 29 			init: function(el, params) {
 30 				this.el = el;
 31 				this.params = params || {};
 32 
 33 				this.params.url = $we.utils.setValue(this.params.url, _defaultUrl);
 34 				this.params.formAfter = [{
 35 					label: '输入新密码',
 36 					type: 'password',
 37 					name: 'new_pwd',
 38 					expression: function(e) {
 39 						var s=e.val();
 40 						o = $we.pwdStrength(s);
 41 		                if(o.errno != 0){
 42 		                    return {valid:false,error_msg:o.msg};
 43 		                }else{
 44 		                    return {valid:true};
 45 		                }
 46 					},
 47 					info: '由6-30位字母和数字组成,不区分大小写,密码不可与用户名相同',
 48 					events: "blur",
 49 					require: "true"
 50 				}, {
 51 					label: '确认新密码',
 52 					type: 'password',
 53 					name: 'confirm_pwd',
 54 					expression: 'equal-new_pwd',
 55 					events: "blur",
 56 					require: "true"
 57 				}];
 58 				var me = this;
 59 				this.params.formCommit = function() {
 60 					me.notify("next");
 61 				}
 62 			},
 63 			/**
 64 			 * interfaces
 65 			 * @memberOf $we.widget.modify_password_by_phone#
 66 			 */
 67 			interfaces: {
 68 				/**
 69 				 * render
 70 				 * @memberOf $we.widget.modify_password_by_phone#
 71 				 */
 72 				render: function() {
 73 					this.tips = $we.widget.add("process.tips.small", this.el, {
 74 						content: "为了保护您账号的安全,请不要将G家的密码设为与其它网站(如:其它游戏、邮箱、聊天工具等)相同的密码。",
 75 						noMt: true
 76 					});
 77 
 78 					if ($we.process.getData("independent")) {
 79 						if ($we.biz.use) this.append(this.el, _changeProcessHtml);
 80 					}
 81 
 82 					this.append(this.el, _html);
 83 					this.extend("comp.verify_code", [this.node.root, this.params]);
 84 
 85 					this._super.render();
 86 				},
 87 				/**
 88 				 * 检测是否合法
 89 				 * @memberOf $we.widget.modify_password_by_phone#
 90 				 */
 91 				checkValid: function() {
 92 					if (!this.form.valid())
 93 						return false;
 94 
 95 					return true;
 96 						
 97 				},
 98 				/**
 99 				 * 修改密码
100 				 * @memberOf $we.widget.modify_password_by_phone#
101 				 */
102 				modifyPassword: function(cb) {
103 					if (this.sendingRequest) return;
104 
105 					this.sendingRequest = true;
106 					var me = this,
107 						new_pwd = $we.rsa.encrypt(this.form.getValue("new_pwd"));
108 					$we.utils.request(this.params.url, {
109 						newpwd: new_pwd,
110 						key: this.vcodeKey,
111 						vcode: $.trim(this.form.getValue("code"))
112 					}, function(data) {
113 						$.removeCookie(me.params.cookieName);
114 						cb();	
115 						me.sendingRequest = false;
116 					}, function(data) {
117 						if (parseInt(data.errno) == -2000) {
118 							me.form.showError("code", data.msg || "");
119 						} else {
120 							me.form.showError("confirm_pwd", data.msg || "");	
121 						}
122 						me.sendingRequest = false;
123 					}, "POST", true);
124 				}
125 			},
126 			/**
127 			 * events
128 			 * @memberOf $we.widget.modify_password_by_phone-
129 			 */
130 			events: {
131 				/**
132 				 * 切换流程
133 				 * @memberOf $we.widget.modify_password_by_phone-
134 				 */
135 				changeProcess: function() {
136 					this.notify("close");
137 					// 如果是手机账号,那么切换流程的时候,把手机账号的提示的过程给跳过,然后再置回来
138 					var isMainAccount = !!$we.process.getData("is_mobile_acc");
139 					if (isMainAccount)
140 						$we.process.setData("is_mobile_acc", false);
141 					$we.biz.use("modify_password");
142 					if (isMainAccount)
143 						$we.process.setData("is_mobile_acc", true);
144 				}
145 			},
146 			/**
147 			 * process
148 			 * @memberOf $we.widget.modify_password_by_phone#
149 			 */
150 			process: {
151 				/**
152 				 * start
153 				 * @memberOf $we.widget.modify_password_by_phone#
154 				 */
155 				start: function() {
156 					this.render();
157 				},
158 				/**
159 				 * end
160 				 * @memberOf $we.widget.modify_password_by_phone#
161 				 */
162 				end: function() {
163 					$(this.node.root).remove();
164 					this.tips.remove();
165 					$(this.node.changeBtn).remove();
166 				},
167 				/**
168 				 * checkSucc
169 				 * @memberOf $we.widget.modify_password_by_phone#
170 				 */
171 				checkSucc: function() {
172 					if (this.checkValid()) {
173 						var me = this;
174 						this.modifyPassword(function() {
175 							me.notify("goNext");
176 						});
177 					}
178 				}
179 			}
180 		});
181 
182 		return $we.widget.amd("modify_password_by_phone");
183 	}
184 );