Memberaccount.php 19 KB

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