Connectapi.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace app\common\logic;
  3. /**
  4. * ============================================================================
  5. *
  6. * ============================================================================
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * ============================================================================
  11. * 逻辑层模型
  12. */
  13. class Connectapi
  14. {
  15. public function smsRegister($phone, $captcha, $password, $client, $inviter_id = 0)
  16. {
  17. if ($this->check_captcha($phone, $captcha)) {
  18. if (config('ds_config.sms_register') != 1) {
  19. return array('state' => 0, 'msg' => '系统没有开启手机注册功能');
  20. }
  21. $member_model = model('member');
  22. $member_name = 'phone_' . random(10);
  23. $member = $member_model->getMemberInfo(array('member_name' => $member_name)); //检查重名
  24. if (!empty($member)) {
  25. return array('state' => 0, 'msg' => '用户名已被注册');
  26. }
  27. $member = $member_model->getMemberInfo(array('member_mobile' => $phone)); //检查手机号是否已被注册
  28. if (!empty($member)) {
  29. return array('state' => 0, 'msg' => '手机号已被注册');
  30. }
  31. $member = array();
  32. $member['member_name'] = $member_name;
  33. $member['member_password'] = $password;
  34. $member['member_mobile'] = $phone;
  35. $member['member_email'] = '';
  36. $member['member_mobilebind'] = 1;
  37. $member['inviter_id'] = $inviter_id;
  38. $insert_id = $member_model->addMember($member);
  39. if ($insert_id) {
  40. $member_model->addMemberAfter($insert_id, $member);
  41. $member = $member_model->getMemberInfo(array('member_mobile' => $phone));
  42. $key = $member_model->getBuyerToken($member['member_id'], $member['member_name'], $client);
  43. return array('state' => 1, 'username' => $member_name, 'token' => $key, 'info' => $member);
  44. } else {
  45. return array('state' => 0, 'msg' => '注册失败', $member);
  46. }
  47. }
  48. }
  49. /**
  50. * 手机找回密码
  51. * @param array $order_info
  52. * @param string $phone 手机号码
  53. * @param string $password 密码
  54. * @return array
  55. */
  56. public function smsPassword($phone, $captcha, $password, $client)
  57. {
  58. if (config('ds_config.sms_password') != 1) {
  59. return array('state' => 0, 'msg' => '系统没有开启手机找回密码功能');
  60. }
  61. $condition = array();
  62. $condition[] = array('smslog_phone', '=', $phone);
  63. $condition[] = array('smslog_captcha', '=', $captcha);
  64. $condition[] = array('smslog_type', '=', 3);
  65. $smslog_model = model('smslog');
  66. $sms_log = $smslog_model->getSmsInfo($condition);
  67. if (empty($sms_log) || ($sms_log['smslog_smstime'] < TIMESTAMP - 1800)) { //半小时内进行验证为有效
  68. return array('state' => 0, 'msg' => '动态码错误或已过期,重新输入');
  69. }
  70. $member_model = model('member');
  71. $member = $member_model->getMemberInfo(array('member_mobile' => $phone)); //检查手机号是否已被注册
  72. if (!empty($member)) {
  73. $new_password = md5($password);
  74. $member_model->editMember(array('member_id' => $member['member_id']), array('member_password' => $new_password), $member['member_id']);
  75. $member_model->createSession($member); //自动登录
  76. if (!$member['member_state']) {
  77. return array('state' => 0, 'msg' => lang('login_index_account_stop'));
  78. }
  79. $key = $member_model->getBuyerToken($member['member_id'], $member['member_name'], $client);
  80. return array('state' => 1, 'msg' => '密码修改成功', 'token' => $key, 'info' => $member);
  81. }
  82. }
  83. public function getStateInfo()
  84. {
  85. $data['sms_register'] = config('ds_config.sms_register') == 1 ? 1 : 0;
  86. $data['sms_login'] = config('ds_config.sms_login') == 1 ? 1 : 0;
  87. $data['sms_password'] = config('ds_config.sms_password') == 1 ? 1 : 0;
  88. return $data;
  89. }
  90. /**
  91. * 手机验证码验证
  92. */
  93. protected function check_captcha($phone, $captcha, $type = '1')
  94. {
  95. if (strlen($phone) == 11 && strlen($captcha) == 6) {
  96. $condition = array();
  97. $condition[] = array('smslog_phone', '=', $phone);
  98. $condition[] = array('smslog_captcha', '=', $captcha);
  99. $condition[] = array('smslog_type', '=', $type);
  100. $smslog_model = model('smslog');
  101. $sms_log = $smslog_model->getSmsInfo($condition);
  102. if (empty($sms_log) || ($sms_log['smslog_smstime'] < TIMESTAMP - 1800)) { //半小时内进行验证为有效
  103. $state = '动态码错误或已过期,重新输入';
  104. ds_json_encode('10001', $state);
  105. }
  106. return true;
  107. }
  108. return false;
  109. }
  110. /**
  111. * 微信注册
  112. * @param type $reg_info
  113. * @param type $reg_type 自动注册类型 wx qq sina
  114. * @return type
  115. */
  116. public function wx_register($reg_info, $reg_type)
  117. {
  118. $reg_info['nickname'] = isset($reg_info['nickname']) ? $reg_info['nickname'] : '';
  119. $reg_info['nickname'] = removeEmoji($reg_info['nickname']);
  120. $member = array();
  121. $member_model = model('member');
  122. if ($reg_type == 'wx' && !empty($reg_info['member_wxunionid'])) {
  123. //如果用户存在.
  124. $exist_member = $member_model->getMemberInfo(array('member_wxunionid' => $reg_info['member_wxunionid']));
  125. if (!empty($exist_member)) {
  126. return $exist_member;
  127. }
  128. $member['member_wxunionid'] = $reg_info['member_wxunionid'];
  129. $member['member_wxopenid'] = $reg_info['member_wxopenid'];
  130. $member['member_wxinfo'] = serialize($reg_info);
  131. } elseif ($reg_type == 'qq' && !empty($reg_info['member_qqopenid'])) {
  132. //如果用户存在.
  133. $exist_member = $member_model->getMemberInfo(array('member_qqopenid' => $reg_info['member_qqopenid']));
  134. if (!empty($exist_member)) {
  135. return $exist_member;
  136. }
  137. $member['member_qqopenid'] = $reg_info['member_qqopenid'];
  138. $member['member_qqinfo'] = serialize($reg_info);
  139. } elseif ($reg_type == 'sina' && !empty($reg_info['member_sinaopenid'])) {
  140. //如果用户存在.
  141. $exist_member = $member_model->getMemberInfo(array('member_sinaopenid' => $reg_info['member_sinaopenid']));
  142. if (!empty($exist_member)) {
  143. return $exist_member;
  144. }
  145. $member['member_sinaopenid'] = $reg_info['member_sinaopenid'];
  146. $member['member_sinainfo'] = serialize($reg_info);
  147. } else {
  148. return;
  149. }
  150. $member['member_password'] = rand(100000, 999999);
  151. $member['member_email'] = '';
  152. $member['member_birthday'] = TIMESTAMP;
  153. $member['member_nickname'] = $reg_info['nickname'];
  154. if (isset($reg_info['inviter_id'])) {
  155. $member['inviter_id'] = $reg_info['inviter_id'];
  156. }
  157. /*
  158. $rand = rand(100, 899);
  159. if (empty($reg_info['nickname']))
  160. $reg_info['nickname'] = 'name_' . $rand;
  161. if (strlen($reg_info['nickname']) < 3)
  162. $reg_info['nickname'] = $reg_info['nickname'] . $rand;
  163. $member_name = $reg_info['nickname'];
  164. */
  165. $member_name = $reg_type . '_' . random(10);
  166. $member_info = $member_model->getMemberInfo(array('member_name' => $member_name));
  167. if (empty($member_info)) {
  168. $member['member_name'] = $member_name;
  169. $insert_id = $member_model->addMember($member);
  170. $member_info = $member_model->getMemberInfo(array('member_name' => $member_name));
  171. } else {
  172. for ($i = 1; $i < 999; $i++) {
  173. /*
  174. $rand += $i;
  175. $member_name = $reg_info['nickname'] . $rand;
  176. */
  177. $member_name = $reg_type . '_' . random(10);
  178. $member_info = $member_model->getMemberInfo(array('member_name' => $member_name));
  179. if (empty($member_info)) { //查询为空表示当前会员名可用
  180. $member['member_name'] = $member_name;
  181. $insert_id = $member_model->addMember($member);
  182. break;
  183. }
  184. }
  185. }
  186. if ($insert_id) {
  187. $member_model->addMemberAfter($insert_id, $member_info);
  188. if (0 && isset($reg_info['headimgurl'])) { #提高体验暂时不对图片进行处理
  189. $headimgurl = $reg_info['headimgurl'];
  190. $avatar = @copy($headimgurl, BASE_UPLOAD_PATH . '/' . ATTACH_AVATAR . "/avatar_$insert_id.jpg");
  191. if ($avatar) {
  192. $member_model->editMember(array('member_id' => $insert_id), array('member_avatar' => "avatar_$insert_id.jpg"), $insert_id);
  193. }
  194. }
  195. $member = $member_model->getMemberInfo(array('member_id' => $insert_id));
  196. return $member;
  197. }
  198. }
  199. }