Memberaccount.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  9. * 网站地址: https://www.valimart.net/
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 用户账号控制器
  14. */
  15. class Memberaccount extends MobileMember
  16. {
  17. public function initialize()
  18. {
  19. parent::initialize(); // TODO: Change the autogenerated stub
  20. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberpoints.lang.php');
  21. }
  22. /**
  23. * @api {POST} api/Memberaccount/bind_mobile_step1 绑定手机第一步
  24. * @apiVersion 1.0.0
  25. * @apiGroup MemberAccount
  26. *
  27. * @apiHeader {String} X-DS-KEY 用户授权token
  28. *
  29. * @apiParam {String} mobile 手机号
  30. *
  31. * @apiSuccess {String} code 返回码,10000为成功
  32. * @apiSuccess {String} message 返回消息
  33. * @apiSuccess {Object} result 返回数据
  34. * @apiSuccess {Int} result.sms_time 倒计时时间(秒)
  35. */
  36. public function bind_mobile_step1()
  37. {
  38. if (!input('post.mobile') || !preg_match('/^\d{11}$/', input('post.mobile'))) {
  39. ds_json_encode(10001, lang('fill_your_phone_number_correctly'), ['code' => '']);
  40. }
  41. $member_model = model('member');
  42. $check_mobile = $member_model->getMemberInfo(array(
  43. 'member_mobile' => trim(input('post.mobile')),
  44. 'member_mobilebind' => 1
  45. ));
  46. if (is_array($check_mobile) and count($check_mobile) > 0) {
  47. ds_json_encode(10001, lang('please_change_another_phone_number'), ['code' => '']);
  48. }
  49. //验证发送频率
  50. $verify_code_model = model('verify_code');
  51. $result = $verify_code_model->isVerifyCodeFrequant(4, 1);
  52. if (!$result['code']) {
  53. ds_json_encode(10001, $result['msg'], ['code' => '']);
  54. }
  55. $verify_code = $verify_code_model->genVerifyCode(4, 1);
  56. if (!$verify_code) {
  57. ds_json_encode(10001, lang('system_occur_error'), ['code' => '']);
  58. }
  59. try {
  60. $mailtemplates_model = model('mailtemplates');
  61. $tpl_info = $mailtemplates_model->getTplInfo(array('mailmt_code' => 'modify_mobile'));
  62. $param = array();
  63. $param['code'] = $verify_code;
  64. $ten_param=array($param['code']);
  65. $message = ds_replace_text($tpl_info['mailmt_content'], $param);
  66. $smslog_param=array(
  67. 'ali_template_code'=>$tpl_info['ali_template_code'],
  68. 'ali_template_param'=>$param,
  69. 'ten_template_code'=>$tpl_info['ten_template_code'],
  70. 'ten_template_param'=>$ten_param,
  71. 'message'=>$message,
  72. );
  73. //发送短信
  74. $result = model('smslog')->sendSms(input('post.mobile'), $smslog_param,4,$verify_code);
  75. if ($result['state']) {
  76. $ip = request()->ip();
  77. $flag = $verify_code_model->addVerifyCode(array(
  78. 'verify_code_type' => 4,
  79. 'verify_code' => $verify_code,
  80. 'verify_code_user_type' => 1,
  81. 'verify_code_user_id' => $this->member_info['member_id'],
  82. 'verify_code_user_name' => $this->member_info['member_name'],
  83. 'verify_code_add_time' => TIMESTAMP,
  84. 'verify_code_ip' => $ip,
  85. ));
  86. if (!$flag) {
  87. ds_json_encode(10001, lang('system_occur_error'));
  88. }
  89. $updates = array();
  90. $updates['member_mobilebind'] = 0;
  91. $updates['member_mobile'] = input('post.mobile');
  92. $member_model->editMember(array('member_id' => $this->member_info['member_id']), $updates,$this->member_info['member_id']);
  93. ds_json_encode(10000, lang('send_success'), array('sms_time' => DEFAULT_CONNECT_SMS_TIME));
  94. } else {
  95. ds_json_encode(10001, $result['message']);
  96. }
  97. } catch (Exception $e) {
  98. ds_json_encode(10001, $e->getMessage());
  99. }
  100. }
  101. /**
  102. * @api {POST} api/Memberaccount/bind_mobile_step2 绑定手机第二步 - 验证短信码
  103. * @apiVersion 1.0.0
  104. * @apiGroup MemberAccount
  105. *
  106. * @apiHeader {String} X-DS-KEY 用户授权token
  107. *
  108. * @apiParam {String} auth_code 验证码
  109. *
  110. * @apiSuccess {String} code 返回码,10000为成功
  111. * @apiSuccess {String} message 返回消息
  112. */
  113. public function bind_mobile_step2()
  114. {
  115. if (!input('post.auth_code') || !preg_match('/^\d{6}$/', input('post.auth_code'))) {
  116. ds_json_encode(10001, lang('input_verification_code'), ['code' => '-1']);
  117. }
  118. $member_model = model('member');
  119. $verify_code = input('post.auth_code');
  120. $validate_data = array(
  121. 'verify_code' => $verify_code,
  122. );
  123. $verify_code_validate = ds_validate('verify_code');
  124. if (!$verify_code_validate->scene('verify_code_search')->check($validate_data)) {
  125. ds_json_encode(10001, $verify_code_validate->getError(), ['code' => '-1']);
  126. }
  127. $verify_code_model = model('verify_code');
  128. if (!$verify_code_model->getVerifyCodeInfo(array(array('verify_code_type' ,'=', 4), array('verify_code_user_type' ,'=', 1), array('verify_code_user_id' ,'=', $this->member_info['member_id']), array('verify_code' ,'=', $verify_code), array('verify_code_add_time','>', TIMESTAMP - VERIFY_CODE_INVALIDE_MINUTE * 60)))) {
  129. ds_json_encode(10001, lang('validation_fails'), ['code' => '-1']);
  130. }
  131. $updates = array();
  132. $updates['member_mobilebind'] = 1;
  133. $update = $member_model->editMember(array('member_id' => $this->member_info['member_id']), $updates,$this->member_info['member_id']);
  134. if (!$update) {
  135. ds_json_encode(10001, lang('system_occur_error'));
  136. }
  137. ds_json_encode(10000, lang('ds_common_op_succ'), 1);
  138. }
  139. /**
  140. * @api {POST} api/Memberaccount/bind_email_step1 绑定邮箱第一步
  141. * @apiVersion 1.0.0
  142. * @apiGroup MemberAccount
  143. *
  144. * @apiHeader {String} X-DS-KEY 用户授权token
  145. *
  146. * @apiParam {String} email 邮箱
  147. *
  148. * @apiSuccess {String} code 返回码,10000为成功
  149. * @apiSuccess {String} message 返回消息
  150. * @apiSuccess {Object} result 返回数据
  151. * @apiSuccess {Int} result.sms_time 倒计时时间(秒)
  152. */
  153. public function bind_email_step1()
  154. {
  155. if (!input('post.email') || !preg_match('/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/', input('post.email'))) {
  156. ds_json_encode(10001, lang('please_fill_mailbox_correctly'), ['code' => '']);
  157. }
  158. $member_model = model('member');
  159. $check_email = $member_model->getMemberInfo(array(
  160. 'member_email' => trim(input('post.email')),
  161. 'member_emailbind' => 1
  162. ));
  163. if (is_array($check_email) and count($check_email) > 0) {
  164. ds_json_encode(10001, lang('mailbox_has_been_used'), ['code' => '']);
  165. }
  166. //验证发送频率
  167. $verify_code_model = model('verify_code');
  168. $result = $verify_code_model->isVerifyCodeFrequant(4, 1);
  169. if (!$result['code']) {
  170. ds_json_encode(10001, $result['msg'], ['code' => '']);
  171. }
  172. $verify_code = $verify_code_model->genVerifyCode(4, 1);
  173. if (!$verify_code) {
  174. ds_json_encode(10001, lang('system_occur_error'), ['code' => '']);
  175. }
  176. try {
  177. $uid = base64_encode(ds_encrypt($this->member_info['member_id'] . ' ' . input('post.email')));
  178. $verify_url = HOME_SITE_URL . '/Login/bind_email.html?uid=' . $uid . '&hash=' . md5($verify_code);
  179. $mailtemplates_model = model('mailtemplates');
  180. $tpl_info = $mailtemplates_model->getTplInfo(array('mailmt_code' => 'bind_email'));
  181. $param = array();
  182. $param['site_name'] = config('ds_config.site_name');
  183. $param['user_name'] = $this->member_info['member_name'];
  184. $param['verify_url'] = $verify_url;
  185. $subject = ds_replace_text($tpl_info['mailmt_title'], $param);
  186. $message = ds_replace_text($tpl_info['mailmt_content'], $param);
  187. //发送短信
  188. $email = new \sendmsg\Email();
  189. $result['state'] = $email->send_sys_email(input('post.email'), $subject, $message);
  190. if ($result['state']) {
  191. $ip = request()->ip();
  192. $flag = $verify_code_model->addVerifyCode(array(
  193. 'verify_code_type' => 5,
  194. 'verify_code' => $verify_code,
  195. 'verify_code_user_type' => 1,
  196. 'verify_code_user_id' => $this->member_info['member_id'],
  197. 'verify_code_user_name' => $this->member_info['member_name'],
  198. 'verify_code_add_time' => TIMESTAMP,
  199. 'verify_code_ip' => $ip,
  200. ));
  201. if (!$flag) {
  202. ds_json_encode(10001, lang('system_occur_error'));
  203. }
  204. $updates = array();
  205. $updates['member_emailbind'] = 0;
  206. $updates['member_email'] = input('post.email');
  207. $member_model->editMember(array('member_id' => $this->member_info['member_id']), $updates,$this->member_info['member_id']);
  208. ds_json_encode(10000, lang('send_success'), array('sms_time' => DEFAULT_CONNECT_SMS_TIME));
  209. } else {
  210. ds_json_encode(10001, $result['message']);
  211. }
  212. } catch (Exception $e) {
  213. ds_json_encode(10001, $e->getMessage());
  214. }
  215. }
  216. /**
  217. * 检测会员手机是否绑定
  218. * 更改绑定手机 第一步 - 得到已经绑定的手机号
  219. * 修改密码 第一步 - 得到已经绑定的手机号
  220. * 修改支付密码 第一步 - 得到已经绑定的手机号
  221. */
  222. public function get_mobile_info()
  223. {
  224. $data = array();
  225. $data['state'] = $this->member_info['member_mobilebind'] ? true : false;
  226. $data['mobile'] = $data['state'] ? encrypt_show($this->member_info['member_mobile'], 4, 4) : $this->member_info['member_mobile'];
  227. ds_json_encode(10000, '', $data);
  228. }
  229. /**
  230. * 检测是否设置了支付密码
  231. */
  232. public function get_paypwd_info()
  233. {
  234. $data = array();
  235. $data['state'] = $this->member_info['member_paypwd'] ? true : false;
  236. ds_json_encode(10000, '', $data);
  237. }
  238. /**
  239. * @api {POST} api/Memberaccount/send_auth_code 统一发送身份验证码
  240. * @apiVersion 1.0.0
  241. * @apiGroup MemberAccount
  242. *
  243. * @apiHeader {String} X-DS-KEY 用户授权token
  244. *
  245. * @apiParam {String} type 类型
  246. *
  247. * @apiSuccess {String} code 返回码,10000为成功
  248. * @apiSuccess {String} message 返回消息
  249. * @apiSuccess {Object} result 返回数据
  250. * @apiSuccess {Int} result.sms_time 倒计时时间(秒)
  251. */
  252. public function send_auth_code()
  253. {
  254. $type = input('post.type');
  255. if (!in_array($type, array('email', 'mobile'))) {
  256. ds_json_encode(10001, lang('param_error'));
  257. }
  258. if ($type == 'mobile' && (!$this->member_info['member_mobilebind'] || !$this->member_info['member_mobile'])) {
  259. ds_json_encode(10001, lang('not_bind_mobile'));
  260. }
  261. if ($type == 'email' && (!$this->member_info['member_emailbind'] || !$this->member_info['member_email'])) {
  262. ds_json_encode(10001, lang('not_bind_email'));
  263. }
  264. $member_model = model('member');
  265. //验证发送频率
  266. $verify_code_model = model('verify_code');
  267. $result = $verify_code_model->isVerifyCodeFrequant(6, 1);
  268. if (!$result['code']) {
  269. ds_json_encode(10001, $result['msg']);
  270. }
  271. $verify_code = $verify_code_model->genVerifyCode(6, 1);
  272. if (!$verify_code) {
  273. ds_json_encode(10001, lang('system_occur_error'));
  274. }
  275. try {
  276. $tpl_info = model('mailtemplates')->getTplInfo(array('mailmt_code' => 'authenticate'));
  277. $param = array();
  278. $ali_template_param=array();
  279. $param['code'] = $verify_code;
  280. $ali_template_param=$param;
  281. $param['site_name'] = config('ds_config.site_name');
  282. $ten_template_param=array($param['code']);
  283. $subject = ds_replace_text($tpl_info['mailmt_title'], $param);
  284. $message = ds_replace_text($tpl_info['mailmt_content'], $param);
  285. if ($type == 'email') {
  286. $email = new \sendmsg\Email();
  287. $result['state'] = $email->send_sys_email($this->member_info["member_email"], $subject, $message);
  288. } elseif ($type == 'mobile') {
  289. $smslog_param=array(
  290. 'ali_template_code'=>$tpl_info['ali_template_code'],
  291. 'ali_template_param'=>$ali_template_param,
  292. 'ten_template_code'=>$tpl_info['ten_template_code'],
  293. 'ten_template_param'=>$ten_template_param,
  294. 'message'=>$message,
  295. );
  296. $result = model('smslog')->sendSms($this->member_info['member_mobile'], $smslog_param,5,$verify_code);
  297. }
  298. if ($result['state']) {
  299. $ip = request()->ip();
  300. $flag = $verify_code_model->addVerifyCode(array(
  301. 'verify_code_type' => 6,
  302. 'verify_code' => $verify_code,
  303. 'verify_code_user_type' => 1,
  304. 'verify_code_user_id' => $this->member_info['member_id'],
  305. 'verify_code_user_name' => $this->member_info['member_name'],
  306. 'verify_code_add_time' => TIMESTAMP,
  307. 'verify_code_ip' => $ip,
  308. ));
  309. if (!$flag) {
  310. ds_json_encode(10001, lang('system_occur_error'));
  311. }
  312. ds_json_encode(10000, lang('send_success'), array('sms_time' => DEFAULT_CONNECT_SMS_TIME, 'state' => '1'));
  313. } else {
  314. ds_json_encode(10001, $result['message'], ['code' => '']);
  315. }
  316. } catch (Exception $e) {
  317. ds_json_encode(10001, $e->getMessage(), ['code' => '']);
  318. }
  319. }
  320. /**
  321. * @api {POST} api/Memberaccount/check_auth_code 统一验证身份验证码
  322. * @apiVersion 1.0.0
  323. * @apiGroup MemberAccount
  324. *
  325. * @apiHeader {String} X-DS-KEY 用户授权token
  326. *
  327. * @apiParam {String} auth_code 验证码
  328. *
  329. * @apiSuccess {String} code 返回码,10000为成功
  330. * @apiSuccess {String} message 返回消息
  331. */
  332. public function check_auth_code()
  333. {
  334. $verify_code = input('post.auth_code');
  335. $validate_data = array(
  336. 'verify_code' => $verify_code,
  337. );
  338. $verify_code_validate = ds_validate('verify_code');
  339. if (!$verify_code_validate->scene('verify_code_search')->check($validate_data)) {
  340. ds_json_encode(10001, $verify_code_validate->getError());
  341. }
  342. $verify_code_model = model('verify_code');
  343. if (!$verify_code_model->getVerifyCodeInfo(array(array('verify_code_type' ,'=', 6), array('verify_code_user_type' ,'=', 1), array('verify_code_user_id' ,'=', $this->member_info['member_id']), array( 'verify_code' ,'=', $verify_code), array('verify_code_add_time','>', TIMESTAMP - VERIFY_CODE_INVALIDE_MINUTE * 60)))) {
  344. ds_json_encode(10001, lang('validation_fails'));
  345. }
  346. ds_json_encode(10000, '', 1);
  347. }
  348. private function _modify_pwd_limit_check()
  349. {
  350. //身份验证后,需要在30分钟内完成修改密码操作
  351. $verify_code_model = model('verify_code');
  352. if (!$verify_code_model->getVerifyCodeInfo(array(array('verify_code_type' ,'=', 6), array('verify_code_user_type' ,'=', 1), array('verify_code_add_time','>', TIMESTAMP - 30 * 60)))) {
  353. ds_json_encode(10001, lang('validation_fails'), ['code' => '']);
  354. }
  355. }
  356. /**
  357. * @api {POST} api/Memberaccount/modify_password 更改密码 - 保存新密码到数据库
  358. * @apiVersion 1.0.0
  359. * @apiGroup MemberAccount
  360. *
  361. * @apiHeader {String} X-DS-KEY 用户授权token
  362. *
  363. * @apiParam {String} password 原密码
  364. * @apiParam {String} password1 新密码
  365. *
  366. * @apiSuccess {String} code 返回码,10000为成功
  367. * @apiSuccess {String} message 返回消息
  368. */
  369. public function modify_password()
  370. {
  371. if (!input('post.password') || !input('post.password1') || input('post.password') != input('post.password1')) {
  372. ds_json_encode(10001, lang('param_error'), ['code' => '']);
  373. }
  374. //身份验证后,需要在30分钟内完成修改密码操作
  375. $this->_modify_pwd_limit_check();
  376. $member_model = model('member');
  377. $update = $member_model->editMember(array('member_id' => $this->member_info['member_id']), array('member_password' => md5(input('post.password'))),$this->member_info['member_id']);
  378. if (!$update) {
  379. ds_json_encode(10001, lang('password_setting_failed'), ['code' => '']);
  380. }
  381. ds_json_encode(10000, lang('ds_common_op_succ'), 1);
  382. }
  383. /**
  384. * @api {POST} api/Memberaccount/modify_password 更改支付密码 - 保存新密码到数据库
  385. * @apiVersion 1.0.0
  386. * @apiGroup MemberAccount
  387. *
  388. * @apiHeader {String} X-DS-KEY 用户授权token
  389. *
  390. * @apiParam {String} password 原密码
  391. * @apiParam {String} password1 新密码
  392. *
  393. * @apiSuccess {String} code 返回码,10000为成功
  394. * @apiSuccess {String} message 返回消息
  395. */
  396. public function modify_paypwd()
  397. {
  398. if (!input('post.password') || !input('post.password1') || input('post.password') != input('post.password1')) {
  399. ds_json_encode(10001, lang('param_error'), ['code' => '']);
  400. }
  401. //身份验证后,需要在30分钟内完成修改密码操作
  402. $this->_modify_pwd_limit_check();
  403. $member_model = model('member');
  404. $update = $member_model->editMember(array('member_id' => $this->member_info['member_id']), array('member_paypwd' => md5(input('post.password'))),$this->member_info['member_id']);
  405. if (!$update) {
  406. ds_json_encode(10001, lang('password_setting_failed'), ['code' => '']);
  407. }
  408. ds_json_encode(10000, lang('ds_common_op_succ'), 1);
  409. }
  410. }