| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- <?php
- namespace app\modules\client\models\v1\pond;
- use app\models\AccountLog;
- use app\models\Goods;
- use app\models\Pond;
- use app\models\PondLog;
- use app\models\PondSetting;
- use app\models\PondUserNum;
- use app\models\User;
- use yii\base\Model;
- class PondForm extends Model
- {
- public $user_id;
- public $store_id;
- public function index()
- {
- $pond_id = Pond::find()->where(['store_id' => $this->store_id])->orderBy('id ASC')->column();
- $pond_num = 6;//大转盘目前固定位数6位
- if (count($pond_id) > $pond_num) {
- //保留最后的6个
- $del_pond_id = array_splice($pond_id, 0, count($pond_id) - $pond_num);
- if ($del_pond_id) {
- Pond::deleteAll(['id' => $del_pond_id]);
- }
- }
- $list = Pond::find()
- ->where([
- 'store_id' => $this->store_id,
- ])->with(['gift' => function ($query) {
- $query->where([
- 'store_id' => $this->store_id,
- 'is_delete' => 0
- ]);
- }])
- ->asArray()->all();
- $setting = PondSetting::findOne(['store_id' => $this->store_id]);
- if ($setting['start_time'] > time() || $setting['end_time'] < time()) {
- return [
- 'code' => 1,
- 'msg' => '不在活动时间范围内',
- ];
- }
- $oppty = 0;
- //根据用户积分计算还能抽几次 向下取整
- if ($setting->integral > 0) {
- $user = get_user();
- $user_integral = $user->integral;
- $oppty = floor_num($user_integral / $setting->integral, 0);
- }
- $win_log = PondLog::find()->alias('pl')
- ->leftJoin(['p' => Pond::tableName()], 'pl.pond_id = p.id')
- ->leftJoin(['u' => User::tableName()], 'pl.user_id = u.id')
- ->leftJoin(['g' => Goods::tableName()], 'pl.gift_id = g.id')
- ->where(['pl.store_id' => $this->store_id, 'pl.type' => 2])
- ->select('u.nickname,u.binding, p.name, g.name as gift_name')
- ->orderBy('pl.id desc')
- ->limit(10)
- ->asArray()
- ->all();
- foreach ($win_log as &$item) {
- $item['name'] = $item['name'] ?: $item['gift_name'];
- }
- return [
- 'code' => 0,
- 'data' => [
- 'setting' => $setting,
- 'oppty' => $oppty,
- 'win_log' => $win_log,
- 'list' => $list,
- ],
- ];
- }
- protected function get_rand($succ, $probability)
- {
- $rand = $this->random_num(1, $probability);
- $winning_rate = 0;
- foreach ($succ as $val) {
- $winning_rate += $val->winning_rate;
- if ($winning_rate > $rand && $val->user_id != $this->user_id) {
- return $val->id;
- }
- }
- }
- public function lottery()
- {
- $setting = PondSetting::findOne(['store_id' => $this->store_id]);
- if ($setting['start_time'] > time() || $setting['end_time'] < time()) {
- return [
- 'code' => 1,
- 'msg' => '活动已结束或未开启'
- ];
- }
- $user = get_user();
- $oppty = 0;
- //根据用户积分计算还能抽几次 向下取整
- if ($setting->integral > 0) {
- $user_integral = $user->integral;
- $oppty = floor_num($user_integral / $setting->integral, 0);
- }
- // $oppty = PondUserNum::find()->where(['store_id' => $this->store_id, 'user_id' => $this->user_id, 'status' => 0])->count();
- if ($oppty <= 0) {
- return [
- 'code' => 1,
- 'msg' => '机会已用完'
- ];
- }
- $pond = Pond::find()->where(['store_id' => $this->store_id])->all();
- $succ = $err = array();
- $probability = $stock = 0;
- foreach ($pond as $v) {
- if ($v->type != 1) {
- if ($v->stock > 0) {
- $succ[$v->id] = $v;
- $probability += $v->winning_rate;
- $stock += $v->stock;
- }
- } else {
- $err[$v->id] = $v;
- }
- }
- $rand = $this->random_num(1, 10000);
- if (empty($err)) {
- if ($stock > 0) {
- $id = $this->get_rand($succ, $probability);
- } else {
- return [
- 'code' => 1,
- 'msg' => '库存不足'
- ];
- }
- } else {
- if ($rand < $probability && $stock > 0) {
- $id = $this->get_rand($succ, $probability);
- } else {
- $id = array_rand($err, 1);
- }
- }
- $form = Pond::findOne([
- 'store_id' => $this->store_id,
- 'id' => $id
- ]);
- // $cishu = PondUserNum::findOne(['store_id' => $this->store_id, 'user_id' => $this->user_id, 'status' => 0]);
- // $cishu->status = 1;
- // $cishu->save();
- $pondLog = new PondLog;
- $pondLog->store_id = $this->store_id;
- $pondLog->user_id = $this->user_id;
- $pondLog->type = $form->type;
- $pondLog->status = 0;
- if ($form->type == 1) {
- $pondLog->num = 0;
- $pondLog->status = 1;
- }
- $pondLog->pond_id = $id;
- $pondLog->pond_name = $form->name;
- $pondLog->gift_id = $form->gift_id;
- $pondLog->create_time = time();
- $t = \Yii::$app->db->beginTransaction();
- if ($form->type != 1) {
- $sql = 'select * from ' . Pond::tableName() . ' where store_id = ' . $this->store_id . ' and id = ' . $id . ' for update';
- $pond = \Yii::$app->db->createCommand($sql)->queryOne();
- //判断库存是否大于0
- if ($pond['stock'] > 0) {
- //将库存数量减1
- $form->stock = $pond['stock'] - 1;
- $pondLog->num = 1;
- if (!$form->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- } else {
- $pondLog->type = 1;
- if (empty($err)) {
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- } else {
- $id = array_rand($err, 1);
- };
- $pondLog->pond_id = $id;
- $pondLog->num = 0;
- $pondLog->gift_id = 0;
- }
- }
- if ($pondLog->save()) {
- //扣减积分
- $result = AccountLog::saveLog(
- $user->id,
- $setting->integral,
- AccountLog::TYPE_INTEGRAL,
- AccountLog::LOG_TYPE_EXPEND,
- AccountLog::TYPE_POND_COMMISSION,
- $pondLog->id,
- '大转盘抽奖扣减积分'
- );
- if (!$result) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '扣减积分失败'
- ];
- }
- $t->commit();
- $array = [
- 'oppty' => $oppty - 1,
- 'id' => $id,
- 'p_id' => $pondLog->id,
- ];
- return [
- 'code' => 0,
- 'data' => (object)$array
- ];
- } else {
- return $this->getErrorResponse($pondLog);
- }
- }
- public function setting()
- {
- $list = PondSetting::findOne(['store_id' => $this->store_id]);
- $list->end_time = date('Y.m.d H', $list->end_time);
- $list->start_time = date('Y.m.d H', $list->start_time);
- if ($list) {
- return [
- 'code' => 0,
- 'msg' => '成功',
- 'data' => $list
- ];
- }
- }
- protected function random_num($min, $max)
- {
- return mt_rand() % ($max - $min + 1) - $min;
- }
- public function qrcode()
- {
- $goods_qrcode_dst = \Yii::$app->basePath . '/web/cyy/images/pond_qrcode.png';
- $font_path = \Yii::$app->basePath . '/web/cyy/font/st-heiti-light.ttc';
- $editor = Grafika::createEditor(GrafikaHelper::getSupportEditorLib());
- $editor->open($goods_qrcode, $goods_qrcode_dst);
- //获取小程序码图片
- $user_id = \Yii::$app->user->id;
- if (\Yii::$app->fromAlipayApp()) {
- $scene = "user_id={$user_id}";
- } else {
- $scene = "user_id:{$user_id}";
- }
- $wxapp_qrcode_file_res = $this->getQrcode($scene, 240, "pond/pond/pond");
- if ($wxapp_qrcode_file_res['code'] == 1) {
- return [
- 'code' => 1,
- 'msg' => '获取小程序码失败,' . $wxapp_qrcode_file_res['msg'],
- ];
- }
- $wxapp_qrcode_file_path = $wxapp_qrcode_file_res['file_path'];
- $editor->open($wxapp_qrcode, $wxapp_qrcode_file_path);
- $user = \Yii::$app->user->identity;
- // 用户头像
- $user_pic_path = $this->saveTempImage($user->avatar_url);
- if (!$user_pic_path) {
- return [
- 'code' => 1,
- 'msg' => '获取海报失败:用户头像丢失',
- ];
- }
- $goods_pic_save_path = \Yii::$app->basePath . '/web/temp/';
- $goods_pic_save_name = md5("v=1.6.2&user_id={$user_id}") . '.jpg';
- list($w, $h) = getimagesize($user_pic_path);
- $user_pic_path = $this->avatar($user_pic_path, $goods_pic_save_path, $w, $h);
- $editor->open($user_pic, $user_pic_path);
- //附加用户头像
- $editor->resizeExactWidth($user_pic, 80);
- $editor->blend($goods_qrcode, $user_pic, 'normal', 1.0, 'top-left', 160, 480);
- //加上文字
- $username = $this->setName($user->nickname);
- $editor->text($goods_qrcode, $username, 25, 272, 490, new Color('#ffffff'), $font_path, 0);
- $editor->text($goods_qrcode, '邀请你一起抽大奖', 25, 272, 530, new Color('#ffffff'), $font_path, 0);
- //加下文字
- $editor->text($goods_qrcode, '扫描二维码', 30, 270, 1064, new Color('#ffffff'), $font_path, 0);
- $editor->text($goods_qrcode, '和我一起抽奖!', 30, 240, 1114, new Color('#ffffff'), $font_path, 0);
- //附加小程序码图片
- $editor->resizeFit($wxapp_qrcode, 400, 400);
- $editor->blend($goods_qrcode, $wxapp_qrcode, 'normal', 1.0, 'top-center', 0, 616);
- //保存图片
- $editor->save($goods_qrcode, $goods_pic_save_path . $goods_pic_save_name, 'jpeg', 85);
- $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/temp/' . $goods_pic_save_name);
- //删除临时图片
- unlink($wxapp_qrcode_file_path);
- unlink($goods_qrcode);
- return [
- 'code' => 0,
- 'data' => [
- 'pic_url' => $pic_url . '?v=' . time(),
- ],
- ];
- }
- private function setName($text)
- {
- if (mb_strlen($text, 'UTF-8') > 8) {
- $text = mb_substr($text, 0, 8, 'UTF-8') . '...';
- }
- return $text;
- }
- //获取网络图片到临时目录
- private function saveTempImage($url)
- {
- $wdcp_patch = false;
- $wdcp_patch_file = \Yii::$app->basePath . '/patch/wdcp.json';
- if (file_exists($wdcp_patch_file)) {
- $wdcp_patch = json_decode(file_get_contents($wdcp_patch_file), true);
- if ($wdcp_patch && in_array(\Yii::$app->request->hostName, $wdcp_patch)) {
- $wdcp_patch = true;
- } else {
- $wdcp_patch = false;
- }
- }
- if ($wdcp_patch) {
- $url = str_replace('http://', 'https://', $url);
- }
- if (!is_dir(\Yii::$app->runtimePath . '/image')) {
- mkdir(\Yii::$app->runtimePath . '/image');
- }
- $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
- $img = curl_exec($ch);
- curl_close($ch);
- $fp = fopen($save_path, 'w');
- fwrite($fp, $img);
- fclose($fp);
- return $save_path;
- }
- //第一步生成圆角图片
- public function avatar($url, $path = './', $w, $h)
- {
- $original_path = $url;
- $dest_path = $path . uniqid() . '.png';
- $src = imagecreatefromstring(file_get_contents($original_path));
- $newpic = imagecreatetruecolor($w, $h);
- imagealphablending($newpic, false);
- $transparent = imagecolorallocatealpha($newpic, 0, 0, 0, 127);
- $r = $w / 2;
- for ($x = 0; $x < $w; $x++) {
- for ($y = 0; $y < $h; $y++) {
- $c = imagecolorat($src, $x, $y);
- $_x = $x - $w / 2;
- $_y = $y - $h / 2;
- if ((($_x * $_x) + ($_y * $_y)) < ($r * $r)) {
- imagesetpixel($newpic, $x, $y, $c);
- } else {
- imagesetpixel($newpic, $x, $y, $transparent);
- }
- }
- }
- imagesavealpha($newpic, true);
- imagepng($newpic, $dest_path);
- imagedestroy($newpic);
- imagedestroy($src);
- unlink($url);
- return $dest_path;
- }
- private function getQrcode($scene, $width = 430, $page = null)
- {
- return GenerateShareQrcode::getQrcode($this->store_id, $scene, $width, $page, false);
- }
- //保存图片内容到临时文件
- private function saveTempImageByContent($content)
- {
- if (!is_dir(\Yii::$app->runtimePath . '/image')) {
- mkdir(\Yii::$app->runtimePath . '/image');
- }
- $save_path = \Yii::$app->runtimePath . '/image/' . md5(base64_encode($content)) . '.jpg';
- $fp = fopen($save_path, 'w');
- fwrite($fp, $content);
- fclose($fp);
- return $save_path;
- }
- }
|