Connectapi.php 9.2 KB

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