RegisterController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\matchmaker\controller;
  3. use app\common\Constant;
  4. use app\matchmaker\model\MatchmakerModel;
  5. use app\matchmaker\model\MatchmakerUserMatingModel;
  6. use app\matchmaker\model\MatchmakerUserModel;
  7. use cmf\controller\HomeBaseController;
  8. class RegisterController extends HomeBaseController
  9. {
  10. /**
  11. * 用户登记
  12. */
  13. public function infoAdd()
  14. {
  15. $id = $this->request->get('id', 0);
  16. if (empty($id)) {
  17. $this->error('参数错误');
  18. }
  19. $matchmaker = MatchmakerModel::get($id);
  20. if (empty($matchmaker)) {
  21. $this->error('红娘不存在');
  22. }
  23. $data = [
  24. 'sex' => Constant::SEX_MATCHMAKER,
  25. 'marry' => Constant::MARRY,
  26. 'high' => Constant::HIGH,
  27. 'weight' => Constant::WEIGHT,
  28. 'education' => Constant::EDUCATION,
  29. 'income' => Constant::INCOME,
  30. 'nation' => Constant::NATION,
  31. 'smoke' => Constant::SMOKE,
  32. 'drink' => Constant::DRINK,
  33. 'family' => Constant::FAMILY,
  34. 'tinyint' => Constant::TINYINT,
  35. 'animal' => Constant::ANIMAL,
  36. ];
  37. foreach ($data as &$v) {
  38. $v = json_encode($v);
  39. unset($v);
  40. }
  41. $data['id'] = $id;
  42. return $this->fetch('', $data);
  43. }
  44. public function infoAddPost()
  45. {
  46. $value = $this->request->post();
  47. $value['birthday'] = strtotime($value['birthday']);
  48. $check = MatchmakerUserModel::get(['mobile' => $value['mobile']]);
  49. if (!empty($check)) {
  50. $this->error('您已填写过,请不要重复填写');
  51. }
  52. $user = MatchmakerUserModel::create($value);
  53. MatchmakerUserMatingModel::create(['user_id' => $user['id'], 'animal' => []]);
  54. $this->success('', '', ['id' => $user['id']]);
  55. }
  56. public function matingAdd()
  57. {
  58. $id = $this->request->param('id');
  59. if (empty($id)) {
  60. $this->error('参数错误');
  61. }
  62. $mating = MatchmakerUserMatingModel::get(['user_id' => $id]);
  63. if (empty($mating['animal'])) {
  64. $mating['animal'] = [];
  65. }
  66. $this->assign('mating', $mating);
  67. $education = Constant::EDUCATION;
  68. $education[] = '无要求';
  69. $data = [
  70. 'min_age' => Constant::MIN_AGE,
  71. 'max_age' => Constant::MAX_AGE,
  72. 'min_high' => Constant::MIN_HIGH,
  73. 'max_high' => Constant::MAX_HIGH,
  74. 'min_weight' => Constant::MIN_WEIGHT,
  75. 'max_weight' => Constant::MAX_WEIGHT,
  76. 'native' => Constant::NATIVE,
  77. 'education' => $education,
  78. 'income' => Constant::COND_INCOME,
  79. 'smoke' => Constant::MATTING_SMOKING,
  80. 'drink' => Constant::MATTING_DRINK,
  81. 'tinyint' => Constant::COND_TINYINT,
  82. 'animal' => Constant::ANIMAL,
  83. ];
  84. foreach ($data as &$v) {
  85. $v = json_encode($v);
  86. unset($v);
  87. }
  88. return $this->fetch('', $data);
  89. }
  90. public function matingAddPost()
  91. {
  92. $param = $this->request->post();
  93. if (empty($param['animal'])) {
  94. $param['animal'] = [];
  95. }
  96. MatchmakerUserMatingModel::update($param);
  97. $this->success('操作成功');
  98. }
  99. public function info()
  100. {
  101. return view();
  102. }
  103. /**
  104. * 图片上传
  105. */
  106. public function imageUpload()
  107. {
  108. $file = $this->request->post('file');
  109. $ext = pathinfo($this->request->post('name'))['extension'];
  110. if (!in_array($ext, ['jpg', 'jpeg', 'png'])) {
  111. $this->error('文件后缀必须为jpg,jpeg,png');
  112. }
  113. if ($file) {
  114. //创建目录
  115. $upload_dir = WEB_ROOT . 'upload/image';
  116. if (!is_dir($upload_dir)) {
  117. @mkdir($upload_dir);
  118. }
  119. $upload_dir = $upload_dir . '/' . date('Ymd');
  120. if (!is_dir($upload_dir)) {
  121. @mkdir($upload_dir);
  122. }
  123. //保存文件
  124. $file_name = $this->_file_name($ext);
  125. $file_path = $upload_dir . '/' . $file_name;
  126. $is_upload = $this->_base64_image_content($file, $file_path);
  127. if (!$is_upload) {
  128. $this->error('上传失败,请重新上传');
  129. }
  130. $avatar = cmf_get_user_avatar_url('image' . '/' . date('Ymd') . '/' . $file_name);
  131. $this->result([
  132. 'avatar' => $avatar,
  133. ], 1);
  134. } else {
  135. $this->error('请上传文件');
  136. }
  137. }
  138. private function _file_name($ext)
  139. {
  140. //生成随机文件名
  141. //定义一个包含大小写字母数字的字符串
  142. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  143. //把字符串分割成数组
  144. $newchars = str_split($chars);
  145. //打乱数组
  146. shuffle($newchars);
  147. //从数组中随机取出15个字符
  148. $chars_key = array_rand($newchars, 15);
  149. //把取出的字符重新组成字符串
  150. $fnstr = '';
  151. for ($i = 0; $i < 15; $i++) {
  152. $fnstr .= $newchars[$chars_key[$i]];
  153. }
  154. //输出文件名并做MD5加密
  155. return md5($fnstr . microtime(true) * 1000) . '.' . $ext;
  156. }
  157. private function _base64_image_content($base64_image_content, $file_path)
  158. {
  159. //匹配出图片的格式
  160. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  161. if (file_put_contents($file_path, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
  162. return true;
  163. } else {
  164. return false;
  165. }
  166. } else {
  167. return false;
  168. }
  169. }
  170. }