Gift.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 礼物接口
  7. */
  8. class Gift extends Api
  9. {
  10. protected $noNeedLogin = ['getGiftList','getGiftType'];
  11. protected $noNeedRight = '*';
  12. public $giftModel;
  13. public $gifttypeModel;
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->giftModel = new \app\common\model\Gift();
  18. $this->gifttypeModel = new \app\common\model\GiftType();
  19. }
  20. /**
  21. * 获取礼物类型
  22. */
  23. public function getGiftType() {
  24. // 获取基本信息
  25. $where = [];
  26. $where["is_show"] = 1;
  27. $giftList = $this->gifttypeModel->field("id,name")->where($where)->order("weight","desc")->select();
  28. $this->success("获取成功!",$giftList);
  29. }
  30. //聊天送礼物
  31. public function givegift_typing() {
  32. // 接口防并发
  33. if (!$this->apiLimit(1, 1)) {
  34. $this->error(__('Operation frequently'));
  35. }
  36. $user_id = input('user_id');// 赠送对象
  37. $gift_id = input('gift_id');// 礼物ID
  38. $number = input('number',1,'intval');//数量
  39. if (!$user_id || !$gift_id || $number < 1)
  40. {
  41. $this->error();
  42. }
  43. // 不可以赠送给自己
  44. if($this->auth->id == $user_id)
  45. {
  46. $this->error("不可以赠送给自己");
  47. }
  48. // 获取礼物信息
  49. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  50. if (!$giftinfo)
  51. {
  52. $this->error("请选择礼物");
  53. }
  54. $giftvalue = bcmul($giftinfo['value'],$number);
  55. //被赠送人信息
  56. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  57. if (!$touserinfo)
  58. {
  59. $this->error("不存在的用户");
  60. }
  61. $money = 0.00;
  62. Db::startTrans();
  63. //需要走计费的规则:男的 || 女的 需要收费的
  64. if ($this->auth->gender == 1 || ($this->auth->gender == 0 && $this->auth->is_cost == 1)) {
  65. // 判断当前用户余额
  66. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  67. if($user_gold < $giftvalue)
  68. {
  69. $this->error("您的金币余额不足");
  70. }
  71. // 添加礼物赠送记录表
  72. $data = [
  73. 'user_id' => $this->auth->id,
  74. 'user_to_id' => $user_id,
  75. 'gift_id' => $giftinfo['id'],
  76. 'gift_name' => $giftinfo['name'],
  77. 'number' => $number,
  78. 'price' => $giftvalue,
  79. 'createtime' => time(),
  80. ];
  81. $log_id = Db::name('gift_user_typing')->insertGetId($data);
  82. if(!$log_id){
  83. Db::rollback();
  84. $this->error('赠送失败');
  85. }
  86. if($giftvalue > 0){
  87. // 扣除当前用户余额
  88. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'gold',-$giftvalue,53,'赠送礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_typing',$log_id);
  89. if($wallet_rs['status'] === false){
  90. Db::rollback();
  91. $this->error($wallet_rs['msg']);
  92. }
  93. // 添加赠送用户余额
  94. $money_to_gold = config('site.money_to_gold');
  95. $gift_plat_scale = config('site.gift_plat_scale');
  96. $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
  97. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  98. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$this->auth->id,'money',$money,54,'获得礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_typing',$log_id,2);
  99. if($wallet_rs['status'] === false){
  100. Db::rollback();
  101. $this->error($wallet_rs['msg']);
  102. }
  103. /* //增加赠送用户上级金币
  104. if ($touserinfo['intro_uid']) {
  105. //获取返利比率
  106. $intro_gift_rebate_rate = (int)config('site.intro_gift_rebate_rate'); //邀请人收礼物返利比率
  107. if ($intro_gift_rebate_rate > 0 && $intro_gift_rebate_rate <= 100) {
  108. //上级获得金币数量
  109. $intro_uid_gold = floor($giftvalue * $intro_gift_rebate_rate / 100);
  110. if ($intro_uid_gold > 0) {
  111. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'gold',$intro_uid_gold,66, '聊天礼物获赠奖励','gift_user_typing',$log_id);
  112. if($intro_result['status']===false)
  113. {
  114. Db::rollback();
  115. $this->error($intro_result['msg']);
  116. }
  117. }
  118. }
  119. }*/
  120. //增加赠送用户上级余额
  121. if ($touserinfo['intro_uid']) {
  122. //获取返利比率
  123. $is_agent = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->value('is_agent');
  124. $intro_income_rebate_rate = $is_agent ? (int)config('site.h_intro_income_rebate_rate') : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  125. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  126. //上级获得金额
  127. $intro_uid_money = number_format($money * $intro_income_rebate_rate / 100, 2, '.', '');
  128. if ($intro_uid_money > 0) {
  129. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'money',$intro_uid_money,68, '邀请人聊天礼物获赠奖励','gift_user_typing',$log_id);
  130. if($intro_result['status']===false)
  131. {
  132. Db::rollback();
  133. $this->error($intro_result['msg']);
  134. }
  135. }
  136. }
  137. }
  138. if ($this->auth->gender == 1 && $touserinfo['gender'] == 0) {
  139. //增加亲密度
  140. $user_intimacy_rs = addintimacy($this->auth->id, $user_id, $giftvalue);
  141. if (!$user_intimacy_rs['status']) {
  142. Db::rollback();
  143. $this->error('您的网络开小差啦~');
  144. }
  145. }
  146. }
  147. }
  148. //tag任务赠送金币
  149. //搭讪奖励
  150. // $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,15);
  151. // if($task_rs === false){
  152. // Db::rollback();
  153. // $this->error('完成任务赠送奖励失败');
  154. // }
  155. Db::commit();
  156. //发送消息
  157. if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  158. $tenim = new \app\api\controller\Tenim;
  159. $tenim->sendMessageToUser($this->auth->id, $user_id, $user_intimacy_rs['level_remark'], 1);
  160. }
  161. $return_data['money'] = $money; //获得金额
  162. $return_data['level_remark'] = isset($user_intimacy_rs) ? $user_intimacy_rs['level_remark'] : ''; //亲密度等级提示语
  163. $this->success('赠送成功', $return_data);
  164. }
  165. //打招呼送礼物
  166. public function givechatgift() {
  167. exit; //作废
  168. // 接口防并发
  169. if (!$this->apiLimit(1, 1)) {
  170. $this->error(__('Operation frequently'));
  171. }
  172. $user_id = input('user_id');// 赠送对象
  173. if (!$user_id) {
  174. $this->error();
  175. }
  176. // 不可以赠送给自己
  177. if($this->auth->id == $user_id) {
  178. $this->error("不可以赠送给自己");
  179. }
  180. //查询是否打过招呼
  181. $count = Db::name('gift_user_greet')->where(['user_id' => $this->auth->id, 'user_to_id' => $user_id])->count('id');
  182. if ($count) {
  183. $this->error('已经打过招呼了');
  184. }
  185. // 获取礼物信息
  186. $giftinfo = Db::name('gift_greet')->find();
  187. if (!$giftinfo) {
  188. $this->error('打招呼礼物异常,请联系管理员');
  189. }
  190. $giftvalue = $giftinfo['value'];
  191. //被赠送人信息
  192. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  193. if (!$touserinfo) {
  194. $this->error("不存在的用户");
  195. }
  196. // 判断当前用户余额
  197. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  198. if($user_gold < $giftvalue) {
  199. $this->error("您的金币余额不足");
  200. }
  201. Db::startTrans();
  202. // 添加礼物赠送记录表
  203. $data = [
  204. 'user_id' => $this->auth->id,
  205. 'user_to_id' => $user_id,
  206. 'gift_id' => $giftinfo['id'],
  207. 'gift_name' => $giftinfo['name'],
  208. 'number' => 1,
  209. 'price' => $giftvalue,
  210. 'createtime' => time(),
  211. ];
  212. $log_id = Db::name('gift_user_greet')->insertGetId($data);
  213. if(!$log_id){
  214. Db::rollback();
  215. $this->error('赠送失败');
  216. }
  217. if($giftvalue > 0){
  218. // 扣除当前用户余额
  219. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'gold',-$giftvalue,57,'赠送礼物:'.$giftinfo["name"],'gift_user_greet',$log_id);
  220. if($wallet_rs['status'] === false){
  221. Db::rollback();
  222. $this->error($wallet_rs['msg']);
  223. }
  224. // 添加赠送用户余额
  225. $money_to_gold = config('site.money_to_gold');
  226. $gift_plat_scale = config('site.gift_plat_scale');
  227. $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
  228. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  229. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$this->auth->id,'money',$money,58,'获得礼物:'.$giftinfo["name"],'gift_user_greet',$log_id,2);
  230. if($wallet_rs['status'] === false){
  231. Db::rollback();
  232. $this->error($wallet_rs['msg']);
  233. }
  234. /*//增加赠送用户上级金币
  235. if ($touserinfo['intro_uid']) {
  236. //获取返利比率
  237. $intro_gift_rebate_rate = (int)config('site.intro_gift_rebate_rate'); //邀请人收礼物返利比率
  238. if ($intro_gift_rebate_rate > 0 && $intro_gift_rebate_rate <= 100) {
  239. //上级获得金币数量
  240. $intro_uid_gold = floor($giftvalue * $intro_gift_rebate_rate / 100);
  241. if ($intro_uid_gold > 0) {
  242. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'gold',$intro_uid_gold,66, '打招呼礼物获赠奖励','gift_user_greet',$log_id);
  243. if($intro_result['status']===false)
  244. {
  245. Db::rollback();
  246. $this->error($intro_result['msg']);
  247. }
  248. }
  249. }
  250. }*/
  251. if ($this->auth->gender == 1 && $touserinfo['gender'] == 0) {
  252. //增加亲密度
  253. $user_intimacy_rs = addintimacy($this->auth->id, $user_id, $giftvalue);
  254. if (!$user_intimacy_rs['status']) {
  255. Db::rollback();
  256. $this->error('您的网络开小差啦~');
  257. }
  258. }
  259. }
  260. //tag任务赠送金币
  261. //搭讪奖励
  262. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,24);
  263. if($task_rs === false){
  264. Db::rollback();
  265. $this->error('完成任务赠送奖励失败');
  266. }
  267. Db::commit();
  268. //发送消息
  269. if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  270. $tenim = new \app\api\controller\Tenim;
  271. $tenim->sendMessageToUser($this->auth->id, $user_id, $user_intimacy_rs['level_remark'], 1);
  272. }
  273. $return_data['money'] = $money; //获得金额
  274. $return_data['level_remark'] = isset($user_intimacy_rs) ? $user_intimacy_rs['level_remark'] : ''; //亲密度等级提示语
  275. $return_data['special'] = one_domain_image($giftinfo['special']);
  276. $this->success('赠送成功', $return_data);
  277. }
  278. //礼物列表
  279. public function giftlist() {
  280. $where['status'] = 1;
  281. $giftList = Db::name('gift')->field('id, name, value, image, special,md5_str')->where($where)->order("sort, value")->select();
  282. $giftList = list_domain_image($giftList,['image','special']);
  283. $this->success("获取成功!",$giftList);
  284. }
  285. }