Memberaccount.php 19 KB

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