Attire.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 装扮商城接口
  7. */
  8. class Attire extends Api
  9. {
  10. protected $noNeedLogin = ['getAttireList'];
  11. protected $noNeedRight = '*';
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. $this->attireModel = new \app\common\model\Attire();
  16. $this->attirebackModel = new \app\common\model\AttireBack();
  17. }
  18. /**
  19. * 获取装扮列表
  20. */
  21. public function getAttireList() {
  22. $type = $this->request->request("type"); // 装扮类型:1=座驾,2=头饰,3=聊天气泡,4=消息尾灯
  23. $page = $this->request->request('page',1); // 分页
  24. $pageNum = $this->request->request('pageNum',10); // 分页
  25. // 分页搜索构建
  26. $pageStart = ($page-1)*$pageNum;
  27. // 获取基本信息
  28. $where = [];
  29. $where["type"] = $type;
  30. $attireList = $this->attireModel->where($where)->limit($pageStart,$pageNum)->select();
  31. if($attireList) {
  32. foreach($attireList as $k => $v) {
  33. }
  34. }
  35. $this->success("获取成功!",$attireList);
  36. }
  37. /**
  38. * 购买并加入我的背包
  39. */
  40. public function attireAddToMyBack() {
  41. $attire_id = $this->request->request("attire_id"); //装扮ID
  42. if (!$attire_id) {
  43. $this->error(__('Invalid parameters'));
  44. }
  45. // 判断用户钻石余额是否充足
  46. $userModel = new \app\common\model\User();
  47. $userInfo = $userModel->where(["id"=>$this->auth->id])->find();
  48. if(!$userInfo) {
  49. $this->error("用户信息获取失败!");
  50. }
  51. // 获取购买装扮需要的价格
  52. $attireInfo = $this->attireModel->where(["id"=>$attire_id])->find();
  53. if(!$attireInfo) {
  54. $this->error("装扮信息获取失败!");
  55. }
  56. if($userInfo["jewel"] < $attireInfo["price"]) {
  57. $this->error("您的余额不足,请先充值!");
  58. }
  59. // 进行购买逻辑
  60. Db::startTrans();
  61. try{
  62. $userjewellogModel = new \app\common\model\UserJewelLog();
  63. // 扣除用户钻石余额
  64. $where = [];
  65. $where["id"] = $this->auth->id;
  66. $res1 = $userModel->where($where)->setDec("jewel",$attireInfo["price"]);
  67. // 添加当前用户钻石流水记录
  68. $res2 = $userjewellogModel->addUserJewelLog($this->auth->id, $attireInfo["price"], "-", $userInfo["jewel"], "购买装扮扣除" . $attireInfo["price"] . "钻石!", 6);
  69. // 添加装扮购买记录
  70. $data = [];
  71. $data["user_id"] = $this->auth->id;
  72. $data["attire_id"] = $attire_id;
  73. $data["price"] = $attireInfo["price"];
  74. $data["attire_name"] = $attireInfo["title"];
  75. $data["createtime"] = time();
  76. $attirebylogModel = new \app\common\model\AttireBuyLog();
  77. $res3 = $attirebylogModel->insertGetId($data);
  78. // 加入我的背包
  79. // $where = [];
  80. // $where["user_id"] = $this->auth->id;
  81. // $where["attire_id"] = $attire_id;
  82. // $attirebackInfo = $this->attirebackModel->where($where)->find();
  83. $data = [];
  84. $data["user_id"] = $this->auth->id;
  85. $data["attire_id"] = $attire_id;
  86. $data["attire_name"] = $attireInfo["title"];
  87. $data["price"] = $attireInfo["price"];
  88. $data["file_image"] = $attireInfo["file_image"];
  89. $data["android_image"] = $attireInfo["android_image"];
  90. $data["gif_image"] = $attireInfo["gif_image"];
  91. $data["limit_day"] = $attireInfo["limit_day"];
  92. $data["type"] = $attireInfo["type"];
  93. $data["createtime"] = time();
  94. // if($attirebackInfo) { // 背包中已有商品
  95. // if($attirebackInfo["is_use"] == 1) { // 商品正在使用中
  96. // if($attirebackInfo["duetime"] >= time()) { // 使用中未到期
  97. // $duetime = $attireInfo["limit_day"]*86400;
  98. // $res4 = $this->attirebackModel->setInc("duetime",$duetime);
  99. // } else { // 使用中已到期
  100. // $res4 = $this->attirebackModel->insert($data);
  101. // }
  102. // } else { // 商品未使用
  103. // $res4 = $this->attirebackModel->insert($data);
  104. // }
  105. // } else { // 背包中没有此商品
  106. // $res4 = $this->attirebackModel->insert($data);
  107. // }
  108. $res4 = $this->attirebackModel->insert($data);
  109. if($res1 && $res2 && $res3 && $res4) {
  110. Db::commit();
  111. $this->success("购买成功!");
  112. } else {
  113. $this->error("购买失败,请稍后重试!");
  114. }
  115. }catch (ValidateException $e) {
  116. Db::rollback();
  117. $this->error($e->getMessage());
  118. } catch (PDOException $e) {
  119. Db::rollback();
  120. $this->error($e->getMessage());
  121. } catch (Exception $e) {
  122. Db::rollback();
  123. $this->error($e->getMessage());
  124. }
  125. }
  126. /**
  127. * 获取我的背包装饰列表
  128. */
  129. public function getBackAttireList() {
  130. $page = $this->request->request('page',1); // 分页
  131. $pageNum = $this->request->request('pageNum',10); // 分页
  132. $type = $this->request->request("type"); // 装扮类型:1=座驾,2=头饰,3=聊天气泡,4=消息尾灯
  133. // 分页搜索构建
  134. $pageStart = ($page-1)*$pageNum;
  135. global $where;
  136. $where = [];$whereOr=[];
  137. $where["user_id"] = $this->auth->id;
  138. $type && $where["type"] = $type;
  139. $whereOr["is_use"] = "0";
  140. $whereOr["duetime"] = ["gt",time()];
  141. $list = $this->attirebackModel
  142. ->field("id,type,file_image,gif_image,attire_name as title,limit_day,duetime,is_use,is_using")
  143. ->where(function ($query) {
  144. global $where;
  145. $query->where($where);
  146. })
  147. ->where(function ($query) {
  148. $query->whereOr(["is_use"=>0,"duetime"=>["gt",time()]]);
  149. })
  150. ->limit($pageStart,$pageNum)
  151. ->select();
  152. if(!$list) {
  153. $this->success("背包空空如也!");
  154. }
  155. foreach($list as $k => $v) {
  156. // 有效期/剩余天数
  157. if($v["is_use"] == 1 && $v["duetime"]>time()) { // 正在使用,未过期
  158. $dtime = "剩余".ceil(($v["duetime"]-time())/86400)."天";
  159. }
  160. if($v["is_use"] == 0) {
  161. $dtime = "有效期".$v["limit_day"]."天";
  162. }
  163. $list[$k]["dtime"] = $dtime;
  164. }
  165. $this->success("获取成功!",$list);
  166. }
  167. /**
  168. * 使用并装上装饰
  169. */
  170. public function toUseAttire() {
  171. $id = $this->request->request("id"); //背包中商品ID
  172. if (!$id) {
  173. $this->error(__('Invalid parameters'));
  174. }
  175. // 获取装饰信息
  176. $where = [];
  177. $where["id"] = $id;
  178. $attirebackInfo = $this->attirebackModel->where($where)->find();
  179. $data = [];
  180. $data["is_use"] = 1;
  181. $data["is_using"] = 1;
  182. $data["duetime"] = time()+$attirebackInfo["limit_day"]*86400;
  183. $res = $this->attirebackModel->update($data,$where);
  184. if($res) {
  185. $this->success("设置成功!");
  186. } else {
  187. $this->success("网络错误,请稍后重试!");
  188. }
  189. }
  190. /**
  191. * 装上
  192. */
  193. public function toUsingAttire() {
  194. $id = $this->request->request("id"); //背包中商品ID
  195. $use_type = $this->request->request("use_type",1); //使用方式:1=装上,2=卸下
  196. if (!$id || !in_array($use_type,[1,2])) {
  197. $this->error(__('Invalid parameters'));
  198. }
  199. // 获取装饰信息
  200. $where = [];
  201. $where["id"] = $id;
  202. $attirebackInfo = $this->attirebackModel->where($where)->find();
  203. if($use_type == 2) {
  204. $attirebackInfo->is_using = 0;
  205. $attirebackInfo->save();
  206. $this->success("设置成功!");
  207. }
  208. if($attirebackInfo["is_use"] != 1) {
  209. $data = [];
  210. $data["is_use"] = 1;
  211. $data["is_using"] = 1;
  212. $data["duetime"] = time()+$attirebackInfo["limit_day"]*86400;
  213. $res = $this->attirebackModel->update($data,$where);
  214. if(!$res) $this->error("使用失败!");
  215. }
  216. Db::startTrans();
  217. try{
  218. // 先取消掉所有的
  219. $res1 = $this->attirebackModel->update(["is_using"=>0],["user_id"=>$this->auth->id,"type"=>$attirebackInfo["type"]]);
  220. // 再设置当前
  221. $res2 = $this->attirebackModel->update(["is_using"=>1],["id"=>$id]);
  222. if($res1 && $res2) {
  223. Db::commit();
  224. $this->success("设置成功!");
  225. } else {
  226. $this->success("网络错误,请稍后重试!");
  227. }
  228. }catch (ValidateException $e) {
  229. Db::rollback();
  230. $this->error($e->getMessage());
  231. } catch (PDOException $e) {
  232. Db::rollback();
  233. $this->error($e->getMessage());
  234. } catch (Exception $e) {
  235. Db::rollback();
  236. $this->error($e->getMessage());
  237. }
  238. }
  239. }