Connect.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. * 第三方登录控制器
  6. */
  7. class Connect extends MobileMall
  8. {
  9. public function initialize()
  10. {
  11. parent::initialize(); // TODO: Change the autogenerated stub
  12. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/login.lang.php');
  13. }
  14. /**
  15. * @api {POST} api/Connect/get_sms_captcha 短信动态码
  16. * @apiVersion 1.0.0
  17. * @apiGroup Connect
  18. *
  19. * @apiParam {String} phone 手机号
  20. * @apiParam {String} type 短信类型 1为注册,2为登录,3为找回密码
  21. *
  22. * @apiSuccess {String} code 返回码,10000为成功
  23. * @apiSuccess {String} message 返回消息
  24. * @apiSuccess {Object} result 返回数据
  25. * @apiSuccess {Int} result.sms_time 发送倒计时
  26. */
  27. public function get_sms_captcha()
  28. {
  29. $state = lang('send_fail');
  30. $sms_mobile = input('param.phone');
  31. if (strlen($sms_mobile) == 11) {
  32. $log_type = input('param.type'); //短信类型:1为注册,2为登录,3为找回密码
  33. $state = 'true';
  34. $member_model = model('member');
  35. $member = $member_model->getMemberInfo(array('member_mobile' => $sms_mobile));
  36. $sms_captcha = rand(100000, 999999);
  37. switch ($log_type) {
  38. case '1':
  39. if (config('ds_config.sms_register') != 1) {
  40. $state = lang('system_obile_registration_function');
  41. }
  42. if (!empty($member)) { //检查手机号是否已被注册
  43. $state = '当前手机号已被注册,请更换其他号码。';
  44. }
  45. $mailmt_code = 'register';
  46. break;
  47. case '2':
  48. if (config('ds_config.sms_login') != 1) {
  49. $state = lang('enable_mobile_phone_login');
  50. }
  51. if (empty($member)) { //检查手机号是否已绑定会员
  52. $state = lang('check_correct_number');
  53. }
  54. $mailmt_code = 'login';
  55. break;
  56. case '3':
  57. if (config('ds_config.sms_password') != 1) {
  58. $state = lang('mobile_back_password');
  59. }
  60. if (empty($member)) { //检查手机号是否已绑定会员
  61. $state = lang('check_correct_number');
  62. }
  63. $mailmt_code = 'reset_password';
  64. break;
  65. default:
  66. $state = lang('param_error');
  67. break;
  68. }
  69. if ($state == 'true') {
  70. $smslog_model = model('smslog');
  71. $mailtemplates_model = model('mailtemplates');
  72. $tpl_info = $mailtemplates_model->getTplInfo(array('mailmt_code' => $mailmt_code));
  73. $param = array();
  74. $param['code'] = $sms_captcha;
  75. $ten_param = array($param['code']);
  76. $message = ds_replace_text($tpl_info['mailmt_content'], $param);
  77. $smslog_param = array(
  78. 'ali_template_code' => $tpl_info['ali_template_code'],
  79. 'ali_template_param' => $param,
  80. 'ten_template_code' => $tpl_info['ten_template_code'],
  81. 'ten_template_param' => $ten_param,
  82. 'message' => $message,
  83. );
  84. $result = $smslog_model->sendSms($sms_mobile, $smslog_param, $log_type, $sms_captcha, $member['member_id'], $member['member_name']);
  85. if ($result['state']) {
  86. ds_json_encode(10000, lang('send_success'), array('sms_time' => 60));
  87. exit;
  88. } else {
  89. $state = $result['message'];
  90. }
  91. }
  92. }
  93. ds_json_encode(10001, $state);
  94. }
  95. /**
  96. * 验证注册动态码
  97. */
  98. public function check_sms_captcha()
  99. {
  100. $state = lang('validation_fails');
  101. $phone = input('param.phone');
  102. $captcha = input('param.captcha');
  103. $log_type = input('param.type');
  104. if (strlen($phone) == 11) {
  105. $state = 'true';
  106. $condition = array();
  107. $condition[] = array('smslog_phone', '=', $phone);
  108. $condition[] = array('smslog_captcha', '=', $captcha);
  109. $condition[] = array('smslog_type', '=', $log_type);
  110. $smslog_model = model('smslog');
  111. $sms_log = $smslog_model->getSmsInfo($condition);
  112. if (empty($sms_log) || ($sms_log['smslog_smstime'] < TIMESTAMP - 1800)) { //半小时内进行验证为有效
  113. $state = lang('dynamic_code_expired');
  114. ds_json_encode(10001, $state);
  115. }
  116. ds_json_encode(10000, '', $state);
  117. }
  118. ds_json_encode(10001, $state);
  119. }
  120. /**
  121. * @api {POST} api/Connect/sms_register 手机注册
  122. * @apiVersion 1.0.0
  123. * @apiGroup Connect
  124. *
  125. * @apiParam {String} phone 手机号
  126. * @apiParam {String} captcha 验证码
  127. * @apiParam {String} password 密码
  128. * @apiParam {Int} inviter_id 推荐人id
  129. * @apiParam {String} client 客户端类型
  130. *
  131. * @apiSuccess {String} code 返回码,10000为成功
  132. * @apiSuccess {String} message 返回消息
  133. * @apiSuccess {Object} result 返回数据
  134. * @apiSuccess {Int} result.state 注册状态 1成功0失败
  135. * @apiSuccess {String} result.username 用户名称
  136. * @apiSuccess {String} result.key 用户token
  137. * @apiSuccess {Object} result.info 用户信息
  138. * @apiSuccess {Int} result.info.member_id 用户ID
  139. * @apiSuccess {Object} result.info.member_name 用户名称
  140. * @apiSuccess {Object} result.info.member_truename 真实姓名
  141. * @apiSuccess {Object} result.info.member_avatar 头像
  142. * @apiSuccess {Object} result.info.member_points 积分
  143. * @apiSuccess {Object} result.info.member_email 邮箱
  144. * @apiSuccess {Object} result.info.member_mobile 手机号
  145. * @apiSuccess {Object} result.info.member_qq QQ
  146. * @apiSuccess {Object} result.info.member_ww 旺旺
  147. */
  148. public function sms_register()
  149. {
  150. if (config('ds_config.sms_register') != 1) {
  151. ds_json_encode(10001, lang('login_register_cancel'));
  152. }
  153. $phone = input('post.phone');
  154. $captcha = input('post.captcha');
  155. $password = input('post.password');
  156. $client = input('post.client');
  157. $inviter_id = intval(input('post.inviter_id'));
  158. $logic_connect_api = model('connectapi', 'logic');
  159. $state_data = $logic_connect_api->smsRegister($phone, $captcha, $password, $client, $inviter_id);
  160. if ($state_data['state'] == '1') {
  161. $state_data['info'] = $this->getMemberUser($state_data['info']);
  162. ds_json_encode(10000, '', $state_data);
  163. } else {
  164. ds_json_encode(10001, $state_data['msg']);
  165. }
  166. }
  167. /**
  168. * 手机验证码登录
  169. */
  170. public function sms_login()
  171. {
  172. $member_mobile = input('post.usermobile');
  173. $mobilecode = input('post.mobilecode');
  174. $client = input('post.client');
  175. if (empty($member_mobile) || empty($mobilecode)) {
  176. ds_json_encode(10001, lang('param_error'));
  177. }
  178. if (config('ds_config.sms_login') != 1) {
  179. ds_json_encode(10001, lang('enable_mobile_phone_login'));
  180. }
  181. $condition = array();
  182. $condition[] = array('smslog_phone', '=', $member_mobile);
  183. $condition[] = array('smslog_captcha', '=', $mobilecode);
  184. $condition[] = array('smslog_type', '=', 2);
  185. $smslog_model = model('smslog');
  186. $sms_log = $smslog_model->getSmsInfo($condition);
  187. if (empty($sms_log) || ($sms_log['smslog_smstime'] < TIMESTAMP - 1800)) { //半小时内进行验证为有效
  188. ds_json_encode(10001, lang('dynamic_code_expired'));
  189. }
  190. $member_model = model('member');
  191. $member = $member_model->getMemberInfo(array('member_mobile' => $member_mobile)); //获取当前手机号的用户信息
  192. if (!empty($member)) {
  193. if (!$member['member_state']) { //1为启用 0 为禁用
  194. ds_json_encode(10001, lang('login_index_account_stop'));
  195. }
  196. $token = $member_model->getBuyerToken($member['member_id'], $member['member_name'], $client);
  197. if ($token) {
  198. $result = array();
  199. $result['token'] = $token;
  200. $result['info'] = $this->getMemberUser($member);
  201. //是否有卖家账户
  202. $seller_model = model('seller');
  203. $seller_info = $seller_model->getSellerInfo(array('member_id' => $member['member_id']));
  204. if ($seller_info) {
  205. $token = Sellerlogin::_get_seller_token($seller_info['seller_id'], $seller_info['seller_name'], $client);
  206. if (!$token) {
  207. ds_json_encode(10001, lang('login_fail'));
  208. }
  209. //读取店铺信息
  210. $store_model = model('store');
  211. $store_info = $store_model->getStoreInfoByID($seller_info['store_id']);
  212. $result['seller_token'] = $token;
  213. $result['seller_info'] = $this->getSellerUser($seller_info, $store_info);
  214. $result['info']['store_id'] = $store_info['store_id'];
  215. }
  216. ds_json_encode(10000, '', $result);
  217. } else {
  218. ds_json_encode(10001, lang('param_error'));
  219. }
  220. } else {
  221. ds_json_encode(10001, lang('mobile_not_exist'));
  222. }
  223. }
  224. /**
  225. * @api {POST} api/Connect/find_password 手机找回密码
  226. * @apiVersion 1.0.0
  227. * @apiGroup Connect
  228. *
  229. * @apiHeader {String} X-DS-KEY 用户授权token
  230. *
  231. * @apiParam {String} phone 手机号
  232. * @apiParam {String} captcha 验证码
  233. * @apiParam {String} password 密码
  234. * @apiParam {String} client 用户端 wap手机端
  235. *
  236. * @apiSuccess {String} code 返回码,10000为成功
  237. * @apiSuccess {String} message 返回消息
  238. * @apiSuccess {Object} result 返回数据
  239. * @apiSuccess {Int} result.state 找回密码状态 1成功0失败
  240. * @apiSuccess {String} result.username 用户名称
  241. * @apiSuccess {String} result.key 用户token
  242. * @apiSuccess {Int} result.info.member_id 用户ID
  243. * @apiSuccess {String} result.info.member_name 用户名称
  244. * @apiSuccess {String} result.info.member_truename 真实姓名
  245. * @apiSuccess {String} result.info.member_avatar 头像
  246. * @apiSuccess {String} result.info.member_points 积分
  247. * @apiSuccess {String} result.info.member_email 邮箱
  248. * @apiSuccess {String} result.info.member_mobile 手机号
  249. * @apiSuccess {String} result.info.member_qq QQ
  250. * @apiSuccess {String} result.info.member_ww 旺旺
  251. */
  252. public function find_password()
  253. {
  254. $phone = input('post.phone');
  255. $captcha = input('post.captcha');
  256. $password = input('post.password');
  257. $client = input('post.client');
  258. $logic_connect_api = model('connectapi', 'logic');
  259. $state_data = $logic_connect_api->smsPassword($phone, $captcha, $password, $client);
  260. if ($state_data['state']) {
  261. unset($state_data['state']);
  262. unset($state_data['msg']);
  263. $state_data['info'] = $this->getMemberUser($state_data['info']);
  264. ds_json_encode(10000, '', $state_data);
  265. } else {
  266. ds_json_encode(10001, $state_data['msg']);
  267. }
  268. }
  269. /**
  270. * 登录开关状态
  271. */
  272. public function get_state()
  273. {
  274. $logic_connect_api = model('connectapi', 'logic');
  275. $state_array = $logic_connect_api->getStateInfo();
  276. $key = input('param.t');
  277. if (trim($key) != '' && array_key_exists($key, $state_array)) {
  278. ds_json_encode(10000, '', $state_array[$key]);
  279. } else {
  280. ds_json_encode(10001, lang('param_error'));
  281. }
  282. }
  283. }